<?php
/**
 * Celebrio YahooWeather
 *
 * @copyright  Copyright (c) 2011 Celebrio Software
 * @package    YahooAPI
 */
namespace Celebrio\YahooAPI;

use Nette\Object;

class YahooWeather extends Object {

    private $xml;
    private $location;
    private $condition;
    private $forecast;

    /**
     * creates GoogleWeather object
     * Do not forget to destroy the object so the Google XML does not stay in memory
     * @param String $place name of the place for forecast
     */
    public function __construct($WOEID, $units = "c") {
	if ( $units != "c" && $units != "f" ) throw new \InvalidStateException("Invalid units for Yahoo Weather API");
	$document = @simplexml_load_file("http://weather.yahooapis.com/forecastrss?w=$WOEID&u=$units");
	if(!$document) {
	    throw new \InvalidStateException ("Could not retrieve data from Yahoo weather forecast server.");
	    return;
	}
	$this->xml = $document->channel;
	$this->location = $this->xml->xpath("yweather:location");
	$this->condition = $this->xml->xpath("item/yweather:condition");
	$this->forecast = $this->xml->xpath("item/yweather:forecast");
	unset($document);
    }

    public function getCity() {
	return $this->location[0]["city"];
    }

    public function getRegion() {
	return $this->location[0]["region"];
    }

    public function getCountry() {
	return $this->location[0]["country"];
    }

    public function getCurrentTemperature() {
	return $this->condition[0]["temp"];
    }

    public function getCurrentTextAndImage() {
	return $this->getWeatherInfo($this->condition[0]["code"]);
    }

    public function getTodayTemperatures() {
	return array("low" => $this->forecast[0]["low"], "high" => $this->forecast[0]["high"]);
    }

    public function getTodayTextAndImage() {
	return $this->getWeatherInfo($this->forecast[0]["code"]);
    }

    public function getTomorrowTemperatures() {
	return array("low" => $this->forecast[1]["low"], "high" => $this->forecast[1]["high"]);
    }

    public function getTomorrowTextAndImage() {
	return $this->getWeatherInfo($this->forecast[1]["code"]);
    }

    private function getWeatherInfo($code) {
	switch($code) {
	    case 0:
		$text = _("Tornado");
		$image = "na";
		break;
	    case 1:
		$text = _("Tropical storm");
		$image = "showers";
		break;
	    case 2:
		$text = _("Hurricane");
		$image = "wind";
		break;
	    case 3:
		$text = _("Severe thunderstorms");
		$image = "thunder-storm";
		break;
	    case 4:
		$text = _("Thunderstorms");
		$image = "thunder-storm";
		break;
	    case 5:
		$text = _("Mixed rain and snow");
		$image = "rainy-snow";
		break;
	    case 6:
		$text = _("Mixed rain and sleet");
		$image = "sleet";
		break;
	    case 7:
		$text = _("Mixed snow and sleet");
		$image = "rainy-snow";
		break;
	    case 8:
		$text = _("Freezing drizzle");
		$image = "freezing-rain";
		break;
	    case 9:
		$text = _("Drizzle");
		$image = "drizzle";
		break;
	    case 10:
		$text = _("Freezing rain");
		$image = "freezing-rain";
		break;
	    case 11:
	    case 12:
		$text = _("Showers");
		$image = "showers";
		break;
	    case 13:
		$text = _("Snow flurries");
		$image = "flurries";
		break;
	    case 14:
		$text = _("Light snow showers");
		$image = "snow-shower";
		break;
	    case 15:
		$text = _("Blowing snow");
		$image = "blowing-snow";
		break;
	    case 16:
		$text = _("Snow");
		$image = "snow";
		break;
	    case 17:
		$text = _("Hail");
		$image = "freezing-rain";
		break;
	    case 18:
		$text = _("Sleet");
		$image = "sleet";
		break;
	    case 19:
		$text = _("Dust");
		$image = "smoke";
		break;
	    case 20:
		$text = _("Foggy");
		$image = "fog";
		break;
	    case 21:
		$text = _("Haze");
		$image = "fog";
		break;
	    case 22:
		$text = _("Smoky");
		$image = "smoke";
		break;
	    case 23:
		$text = _("Blustery");
		$image = "chance-storm";
		break;
	    case 24:
		$text = _("Windy");
		$image = "wind";
		break;
	    case 25:
		$text = _("Cold");
		$image = "fair";
		break;
	    case 26:
		$text = _("Cloudy");
		$image = "cloudy";
		break;
	    case 27:
		$text = _("Mostly cloudy");
		$image = "m-cloudy-night";
		break;
	    case 28:
		$text = _("Mostly cloudy");
		$image = "m-cloudy";
		break;
	    case 29:
		$text = _("Partly cloudy");
		$image = "p-c-night";
		break;
	    case 30:
		$text = _("Partly cloudy");
		$image = "partly-cloudy";
		break;
	    case 31:
		$text = _("Clear");
		$image = "moon";
		break;
	    case 32:
		$text = _("Sunny");
		$image = "sunny";
		break;
	    case 33:
		$text = _("Fair");
		$image = "p-c-night";
		break;
	    case 34:
		$text = _("Fair");
		$image = "fair";
		break;
	    case 35:
		$text = _("Mixed rain and hail");
		$image = "freezing-rain";
		break;
	    case 36:
		$text = _("Hot");
		$image = "hazy";
		break;
	    case 37:
		$text = _("Isolated thunderstorms");
		$image = "chance-storm";
		break;
	    case 38:
	    case 39:
		$text = _("Scattered thunderstorms");
		$image = "chance-storm";
		break;
	    case 40:
		$text = _("Scattered showers");
		$image = "m-c-rain";
		break;
	    case 41:
	    case 43:
		$text = _("Heavy snow");
		$image = "blowing-snow";
		break;
	    case 42:
		$text = _("Scattered snow showers");
		$image = "m-c-snow";
		break;
	    case 44:
		$text = _("Partly cloudy");
		$image = "partly-cloudy";
		break;
	    case 45:
		$text = _("Thundershowers");
		$image = "t-storm-rain";
		break;
	    case 46:
		$text = _("Snow showers");
		$image = "snow-shower";
		break;
	    case 47:
		$text = _("Isolated thundershowers");
		$image = "chance-storm";
		break;
	    default:
		$text = _("Not Available");
		$image = "na";
	}
	return array("text" => $text, "image" => $image);
    }

}